package va.radiomaria.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.JTableHeader;

import va.radiomaria.audio.Mood;

/**
 * Shows the main window of the Radio Maria client GUI.
 */
public class MainView extends JFrame implements Observer{

	private static final long serialVersionUID = -6623237153210842677L;
	private DataModel model;
	private ActionListener controller;
	
	private JTable streamingTable;
	private JTextField moodTextField;
	private MoodPanel moodPanel;
	private LikePanel likePanel;
	private ConsoleTabsPane console;
	
	private JPanel menuBarPanel;
	private JPanel streamingTablePanel;
	private JPanel mainContentPanel;
	private JPanel consolePanel;
	private JPanel musicPanel;
	public static Color BACKGROUND_COLOR = new Color(43,43,43);
	public static Color ACTIVE_BACKGROUND = new Color(95,95,95);
	public static Color ACTIVE_FONT_COLOR = new Color(240, 240, 240);
	public static Color PASSIVE_FONT_COLOR = new Color(220, 220, 220);

	/**
	 * Create the application.
	 */
	public MainView(ActionListener listener) {
		controller = listener;
		model = DataModel.getInstance();
		model.addObserver(this); 
		initialize();
	}

	
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		this.setBounds(100, 100, 800, 600);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		mainContentPanel =  new JPanel(new BorderLayout(2,2));

		initializeMenu();
		initializeStreamingTablePanel();
		initializeConsolePanel();
		initializeMusicPanel();
	}
	
	private void initializeMenu(){
		
		menuBarPanel = new JPanel(new GridLayout(1, 1, 0, 0));
		
		JMenuBar menuBar = new JMenuBar();	
		JMenu networkMenu = new JMenu("Network");
		JMenu musicMenu = new JMenu("Music");
		
		JMenuItem connectItem = new JMenuItem("Connect");
		connectItem.setActionCommand(CommonActionCommand.CONNECT.toString());
		connectItem.addActionListener(controller);
		
		JMenuItem leaveItem = new JMenuItem("Leave");
		leaveItem.setActionCommand(CommonActionCommand.LEAVE.toString());
		leaveItem.addActionListener(controller);
		
		JMenuItem uploadMusicItem = new JMenuItem("Upload Song");
		uploadMusicItem.setActionCommand(CommonActionCommand.UPLOAD_MUSIC.toString());
		uploadMusicItem.addActionListener(controller);
		
		JMenuItem uploadMusicURL = new JMenuItem("Upload URL");
		uploadMusicURL.setActionCommand(CommonActionCommand.UPLOAD_URL.toString());
		uploadMusicURL.addActionListener(controller);
		
		networkMenu.add(leaveItem);
		networkMenu.add(connectItem);
		musicMenu.add(uploadMusicItem);
		musicMenu.add(uploadMusicURL);
		
		menuBar.add(networkMenu);
		menuBar.add(musicMenu);
		
		menuBarPanel.add(menuBar);
		getContentPane().add(menuBarPanel, BorderLayout.PAGE_START);
	}
	
	private void initializeStreamingTablePanel(){
		streamingTablePanel = new JPanel(new BorderLayout());
		streamingTable = new JTable(model.getInformationData(), model.getInformationColumns());
		streamingTable.setBackground(ACTIVE_BACKGROUND);
		streamingTable.setForeground(ACTIVE_FONT_COLOR);
		JTableHeader informationTableHeader = streamingTable.getTableHeader();
		informationTableHeader.setBackground(BACKGROUND_COLOR);
		informationTableHeader.setForeground(PASSIVE_FONT_COLOR);
		streamingTablePanel.add(informationTableHeader, BorderLayout.NORTH);
		streamingTablePanel.add(streamingTable, BorderLayout.CENTER);
		mainContentPanel.add(streamingTablePanel, BorderLayout.CENTER);
		getContentPane().add(mainContentPanel, BorderLayout.CENTER);
	}
	
	private void initializeConsolePanel(){

		JLabel consoleLabel = new JLabel(" Activity Console:");
		consoleLabel.setBackground(BACKGROUND_COLOR);
		consoleLabel.setForeground(PASSIVE_FONT_COLOR);
		consolePanel = new JPanel(new BorderLayout());
		console = new ConsoleTabsPane();
		consolePanel.setBackground(BACKGROUND_COLOR);
		consolePanel.add(consoleLabel, BorderLayout.PAGE_START);
		consolePanel.add(console, BorderLayout.CENTER);
		getContentPane().add(consolePanel, BorderLayout.PAGE_END);
	}
	
	private JButton initializeButton(String content, String iconPath, CommonActionCommand command, Dimension size){
		JButton button = null;
		if(iconPath != null) {
			button = new RoundButton(new  ImageIcon(iconPath));
		}
		else {
			button = new JButton();
		}
		button.setPreferredSize(size);
		if(content != null){
			button.setText(content);
		}
		if(command != null){
			button.setActionCommand(command.toString());
		}
		if(controller != null){
			button.addActionListener(controller);
		}	
		button.setBackground(ACTIVE_BACKGROUND);
		button.setBorder(null);
		return button;
	}
	
	private void initializeMusicPanel(){
		JLabel musicLabel = new JLabel(" Audio Player");
		musicLabel.setForeground(PASSIVE_FONT_COLOR);
		
		//Panel with play & pause
		musicPanel = new JPanel(new BorderLayout());
		musicPanel.setBackground(BACKGROUND_COLOR);
		JPanel musicButtonsPanel = new JPanel();
		musicButtonsPanel.setBackground(BACKGROUND_COLOR);
		musicPanel.add(musicLabel, BorderLayout.PAGE_START);
		musicPanel.add(musicButtonsPanel, BorderLayout.WEST);
		moodPanel = new MoodPanel(controller);
		moodPanel.setBackground(BACKGROUND_COLOR);
		likePanel = new LikePanel(controller);
		musicPanel.add(likePanel, BorderLayout.CENTER);
		musicPanel.add(moodPanel, BorderLayout.EAST);
		mainContentPanel.add(musicPanel, BorderLayout.PAGE_END);

		//Initialize buttons
		JButton playMusicButton = initializeButton(null, "Icons/play.png", CommonActionCommand.PLAY, new Dimension(25,25));
		JButton pauseMusicButton = initializeButton(null, "Icons/pause.png", CommonActionCommand.PAUSE, new Dimension(25, 25));
		JButton nextSongButton = initializeButton(null, "Icons/next.png", CommonActionCommand.NEXT_SONG, new Dimension(25, 25));
		
		//Display JTextField with current genre
		moodTextField = new JTextField();
		moodTextField.setForeground(ACTIVE_FONT_COLOR);
		moodTextField.setBackground(BACKGROUND_COLOR);
		moodTextField.setBorder(null);
		moodTextField.setText(" Mood: " + model.getCurrentMood());
		moodTextField.setPreferredSize(new Dimension(200, 25));
				

		// Add components to the panel
		musicButtonsPanel.add(playMusicButton);
		musicButtonsPanel.add(pauseMusicButton);
		musicButtonsPanel.add(nextSongButton);
		musicButtonsPanel.add(moodTextField);
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		moodTextField.setText("Mood: " + model.getCurrentMood());
		console.update(model.getConsoleOutput());
		repaint();
	}

	public Mood getCurrentMood() {
		return moodPanel.getCurrentMood();
	}
	
	public void resetLikeIcon() {
		likePanel.resetLiked();
	}
}
